package com.pubinfo.passbook.common.service.master.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pubinfo.passbook.common.entity.TSystemMenu;
import com.pubinfo.passbook.common.mapper.TSystemMenuMapper;
import com.pubinfo.passbook.common.mapper.TSystemRoleMapper;
import com.pubinfo.passbook.common.model.dto.system.MenuParam;
import com.pubinfo.passbook.common.model.vo.base.JsonResult;
import com.pubinfo.passbook.common.service.master.TSystemMenuService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 *
 */
@Service
public class TSystemMenuServiceImpl extends ServiceImpl<TSystemMenuMapper, TSystemMenu>
    implements TSystemMenuService {
  
  @Resource
  private TSystemRoleMapper roleMapper;
  
  @Override
  public IPage<TSystemMenu> selectSystemMenu(MenuParam param) {
    IPage<TSystemMenu> iPage = page(param.getPage("create_time"));
    return this.baseMapper.selectSystemMenu(iPage, param);
  }
  
  @Override
  public JsonResult<String> saveSystemMenu(TSystemMenu menu) {
//        String perms = menu.getPerms();
//        if (perms.contains("_")){
//            List<TSystemMenu> tSystemMenus =
//                    this.baseMapper.selectMenuByPerms(perms.substring(0, perms.lastIndexOf("_")));
//            tSystemMenus.removeIf(Objects::isNull);
//            if (tSystemMenus.isEmpty()) return JsonResult.error("上级菜单不存在");
//        }
    this.baseMapper.insert(menu);
    return JsonResult.success("新增成功");
  }
  
  @Override
  public JsonResult<String> removeSystemMenu(Integer menuId) {
//        TSystemMenu menu = this.baseMapper.selectById(menuId);
//        List<TSystemMenu> tSystemMenus =
//                this.baseMapper.selectMenuByPerms(menu.getPerms());
//        tSystemMenus.removeIf(Objects::isNull);
//        if (tSystemMenus.size() > 1) return JsonResult.error("该菜单存在子菜单,删除失败");
    if (this.baseMapper.deleteById(menuId) > 0) {
      //同时删除对应的角色绑定关系
      roleMapper.delPermRole(null, menuId);
      return JsonResult.success("删除成功");
    }
    return JsonResult.error("删除失败");
  }
  
  @Override
  public JsonResult<String> updateSystemMenu(TSystemMenu menu) {
//        String perms = menu.getPerms();
//        if (perms.contains("_")){
//            List<TSystemMenu> tSystemMenus =
//                    this.baseMapper.selectMenuByPerms(perms.substring(0, perms.lastIndexOf("_")));
//            tSystemMenus.removeIf(Objects::isNull);
//            if (tSystemMenus.isEmpty()) return JsonResult.error("上级菜单不存在");
//        }
    this.baseMapper.updateById(menu);
    return JsonResult.success("修改成功");
  }
  
  @Override
  public List<TSystemMenu> selectMenuTree(Integer menuId) {
    Integer id = menuId == null ? 0 : menuId;
    return getChildPerms(this.list(), id);
  }
  
  /**
   * 根据父节点的ID获取所有子节点
   *
   * @param list     分类表
   * @param parentId 传入的父节点ID
   * @return String
   */
  public List<TSystemMenu> getChildPerms(List<TSystemMenu> list, int parentId) {
    List<TSystemMenu> returnList = new ArrayList();
    for (Iterator<TSystemMenu> iterator = list.iterator(); iterator.hasNext(); ) {
      TSystemMenu t = iterator.next();
      // 一、根据传入的某个父节点ID,遍历该父节点的所有子节点
      if (t.getParentId() == parentId) {
        recursionFn(list, t);
        returnList.add(t);
      }
    }
    return returnList;
  }
  
  /**
   * 递归列表
   *
   * @param list
   * @param t
   */
  private void recursionFn(List<TSystemMenu> list, TSystemMenu t) {
    // 得到子节点列表
    List<TSystemMenu> childList = getChildList(list, t);
    t.setChildren(childList);
    for (TSystemMenu tChild : childList) {
      if (hasChild(list, tChild)) {
        recursionFn(list, tChild);
      }
    }
  }
  
  /**
   * 得到子节点列表
   */
  private List<TSystemMenu> getChildList(List<TSystemMenu> list, TSystemMenu t) {
    List<TSystemMenu> tlist = new ArrayList();
    Iterator<TSystemMenu> it = list.iterator();
    while (it.hasNext()) {
      TSystemMenu n = it.next();
      if (n.getParentId().longValue() == t.getMenuId().longValue()) {
        tlist.add(n);
      }
    }
    return tlist;
  }
  
  /**
   * 判断是否有子节点
   */
  private boolean hasChild(List<TSystemMenu> list, TSystemMenu t) {
    return getChildList(list, t).size() > 0;
  }
}




